/**
 * Created by Amaris on 2016/10/8.
 */
var util = require('./util');
var mysqlAdapter = require('sails-mysql');
var conf = require('config');
var contact = require('../po/contact');
var roadShowRange = require('../po/dic-roadshowrange');
var roadShowType = require('../po/dic-roadshowtype');
var strategyMeetingRange = require('../po/dic-strategymeetingrange');
var surveyingRange = require('../po/dic-surveyingrange');
var surveyingType = require('../po/dic-surveyingtype');
var knowledgeBase = require('../po/knowledge-base');
var otherService = require('../po/other-service');
var recommond = require('../po/recommond');
var roadshow = require('../po/roadshow');
var seller = require('../po/seller');
var strategyMeeting = require('../po/strategy-meeting');
var surveying = require('../po/surveying');
var user = require('../po/user');
var evaluate = require('../po/evaluate');
var industry = require('../po/industry');
var company=require('../po/dic-company');
var stock=require('../po/dic-stock');

var company=require('../po/company');
var addressbook=require('../po/addressbook');

var roadshowLocation=require('../po/dic-roadshowlocation');
var stock=require('../po/dic-stock');
var stockRecommend=require('../po/stockrecommend');
var teleConference=require('../po/teleconference');
var userjoin=require('../po/userjoin');

var team = require('../po/team');
var teamUser = require('../po/teamuser');

var stockpoolgroup = require('../po/stockpoolgroup');

var workdiary = require('../po/workdiary');
var comment = require('../po/comment');
var addressbookDept = require('../po/addressbookDept');

var knowledgeCenter = require('../po/knowledgecenter');

var search = require('../po/search');

var stockChannel = require('../po/stockChannel');
var stockGroup = require('../po/stockGroup');
var stockDeal = require('../po/stockDeal');
var stock_history_price = require('../po/stock_history_price');
var stock_position = require('../po/stockPosition');
var stock_group_rate = require('../po/stockGroupRate');
var knowledgegroup = require('../po/knowledgeGroup');

var scheduleStock = require('./scheduleStock');
var scheduleStockGroupRate = require('./scheduleStockGroupRate');
/**
 * 2017年06月19日15:52:14
 * @type {*}
 */
var mconference=require('../po/mconference');

var isInit = false;
var mysqlConfig = conf.get("mysqlConfig");

exports.loadPo = function () {

    if (isInit) return;
    console.log('Init po...');
    util.waterline.loadCollection(contact);
    util.waterline.loadCollection(roadShowRange);
    util.waterline.loadCollection(roadShowType);
    util.waterline.loadCollection(strategyMeetingRange);
    util.waterline.loadCollection(surveyingRange);
    util.waterline.loadCollection(surveyingType);
    util.waterline.loadCollection(knowledgeBase);
    util.waterline.loadCollection(otherService);
    util.waterline.loadCollection(recommond);
    util.waterline.loadCollection(roadshow);
    util.waterline.loadCollection(seller);
    util.waterline.loadCollection(strategyMeeting);
    util.waterline.loadCollection(surveying);
    util.waterline.loadCollection(user);
    util.waterline.loadCollection(industry);
    util.waterline.loadCollection(team);
    util.waterline.loadCollection(teamUser);

    util.waterline.loadCollection(company);
    util.waterline.loadCollection(stock);
    util.waterline.loadCollection(roadshowLocation);
    util.waterline.loadCollection(stock);
    util.waterline.loadCollection(comment);
    util.waterline.loadCollection(stockRecommend);
    util.waterline.loadCollection(teleConference);
    util.waterline.loadCollection(userjoin);
    util.waterline.loadCollection(company);
    util.waterline.loadCollection(addressbook);
    util.waterline.loadCollection(addressbookDept);
    util.waterline.loadCollection(search);
    /**
     *2017年06月19日15:48:55 新增
     */
    util.waterline.loadCollection(mconference);

    /**
     * 视图
     * @type {boolean}
     */
    util.waterline.loadCollection(evaluate);

    util.waterline.loadCollection(stockpoolgroup);

    util.waterline.loadCollection(workdiary);

    util.waterline.loadCollection(knowledgeCenter);

    util.waterline.loadCollection(stockChannel);
    util.waterline.loadCollection(stockGroup);
    util.waterline.loadCollection(stockDeal);
    util.waterline.loadCollection(stock_position);
    util.waterline.loadCollection(stock_group_rate);
    util.waterline.loadCollection(stock_history_price);
    util.waterline.loadCollection(knowledgegroup);


    isInit = true;
};

exports.initDatabase = function () {
    var config = {
        adapters: {
            mysql: mysqlAdapter,
            default: 'mysql'
        },
        connections: {
            mysql: {
                adapter: 'mysql',
                host: mysqlConfig.ip,
                port: mysqlConfig.port,
                database: mysqlConfig.database,
                user: mysqlConfig.user,
                password: mysqlConfig.password,
                charset: 'utf8',
                collation: 'utf8_general_ci'
            }
        },
        defaults: {

             migrate: 'safe'
             //migrate: 'alter'

        }
    };

    util.waterline.initialize(config, function (err, ontology) {
        if (err) {
            return console.error(err);
        }

        util.models = ontology.collections;

        //定时抓取股票历史价格数据
         scheduleStock.init();
        scheduleStockGroupRate.saveStockGroupRate();
        console.log('Init po done!');
    });
};
